package com.sj.purchase.modules.cost.web;

import com.sj.purchase.modules.cost.repository.StockContrastRepository;
import com.sj.purchase.modules.cost.repository.order.InOrderItemRepository;
import com.sj.purchase.modules.cost.repository.order.OutOrderItemRepository;
import com.sj.purchase.modules.cost.domain.StockContrast;
import com.sj.purchase.util.DateUtils;
import com.sj.purchase.util.ExcelDownloadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Alin on 2017/10/23.
 */
@RestController
@RequestMapping(value = "api/stock-contract")
public class StockContrastController {

    private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    InOrderItemRepository inOrderItemRepository;
    @Autowired
    OutOrderItemRepository outOrderItemRepository;
    @Autowired
    StockContrastRepository stockContrastRepository;

    @GetMapping
    public ResponseEntity contrast(@RequestParam(required = false) String day, @RequestParam(defaultValue = "") String sku, @PageableDefault Pageable pageable) throws ParseException {
        Date startDate = null;
        Date endDate = null;
        if(!day.isEmpty()){
            startDate = df.parse(day);
            endDate = DateUtils.getEndOfDay(startDate);
        }
        if(!sku.isEmpty() && !day.isEmpty()){
            return ResponseEntity.ok(stockContrastRepository.findById_SkuNoAndId_CreateDateBetween(sku,startDate,endDate,pageable));
        }
        if (!sku.isEmpty()) {
            return ResponseEntity.ok(stockContrastRepository.findById_SkuNo(sku, pageable));
        }
        return ResponseEntity.ok(stockContrastRepository.findById_CreateDateBetween(startDate,endDate, pageable));
    }


    @GetMapping("excel")
    public void exportExcel(@RequestParam(required = false)String day, @RequestParam(defaultValue = "") String sku) throws ParseException {
        Date startDate = null;
        Date endDate = null;
        if(!day.isEmpty()){
            startDate = df.parse(day);
            endDate = DateUtils.getEndOfDay(startDate);
        }
        if(!sku.isEmpty() && !day.isEmpty()){
            ExcelDownloadUtil.easyPoi("库存对比表", StockContrast.class, stockContrastRepository.findById_SkuNoAndId_CreateDateBetween(sku,startDate,endDate));
        }
        if (!sku.isEmpty()) {
            ExcelDownloadUtil.easyPoi("库存对比表", StockContrast.class, stockContrastRepository.findById_SkuNo(sku));
        }
        List<StockContrast> list = stockContrastRepository.findById_CreateDateBetween(startDate,endDate);
        ExcelDownloadUtil.easyPoi("库存对比表", StockContrast.class,list );
    }
}
